package com.jxud.mapper;

import com.jxud.Pojo.*;
import org.apache.commons.math3.stat.descriptive.summary.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {
    /**
     * 根据token 获取到当前客户的订单 Mapper
     *
     * @param id
     * @return
     */
//    @Select("select * from orders where username = #{id}")
    List<Order> getOrderById(Integer id, PageOrder pageOrder, String wxName);

    /**
     * 获取当前查询的总条数用做分页查询
     *
     * @param id
     * @param pageOrder
     * @return
     */
    int totalOrders(Integer id, PageOrder pageOrder, String wxName);

    /**
     * 根据订单号获取订单的发货信息 Mapper
     *
     * @param orderId
     * @return
     */
//    @Select("select * from order_items where order_id in #{orderId} ORDER BY id ASC")
    List<OrderItems> getOrderItemByOrderId(List<String> orderId);

    /**
     * 插入订单 Mapper
     *
     * @param order
     */
    @Insert("INSERT INTO orders(platform,order_id,receiver_person,receiver_phone,receiver_street,order_status,customer_note,receiver_province,receiver_city,receiver_county,created_at,updated_at,username,roomname" +
            ")VALUES (#{platform},#{orderId},#{receiverPerson},#{receiverPhone},#{receiverStreet},#{orderStatus},#{customerNote},#{receiverProvince},#{receiverCity},#{receiverCounty},#{createdAt},#{updatedAt},#{username},#{roomname})")
    void insertOrder(Order order);

    /**
     * 插入订单明细
     *
     * @param items
     */
    @Insert("INSERT INTO order_items(order_id,product_name,product_code,quantity,unit_price,skuid,is_combo)" +
            "values (#{orderId},#{productName},#{productCode},#{quantity},#{unitPrice},#{skuId},#{isCombo})")
    void insertOrderItems(OrderItems items);

    /**
     * 关闭订单
     *
     * @param orderId
     */
    @Update("UPDATE orders SET order_status= 4 where order_status = 0 and order_id = #{orderId};")
    Integer closeOrderId(String orderId);

    /**
     * 每次下单的商品 累加排名
     *
     * @param productCode
     */
    @Update("UPDATE products SET view_count = view_count+1 where code = #{productCode}")
    void queryProducts(String productCode);

    /**
     * 根据商品编码查询返回商品信息
     */
    @Select("select name,erp_skuid,is_combination from products where code = #{productCode};")
    Products getProductByCode(String productCode);


    /**
     * 订单号是否存在 列表单个 或者多个
     *
     * @param systemOrderId
     * @return
     */
    List<Order> orderExists(List<String> systemOrderId);

    /**
     * 根据订单号单个订单 查询订单信息
     *
     * @return
     */
    @Select("SELECT * FROM orders WHERE order_id  = #{orderId};")
    List<Order> getOrdersByIdDesens(String orderId);

    /**
     * 修改订单状态
     *
     * @param orderId
     * @param status
     */

//    @Update("UPDATE orders SET  #{s} = #{status} WHERE order_id = #{orderId} ")
//    Integer updateOrderStatus(String orderId, Integer status, String s);
    @UpdateProvider(type = OrderSqlProvider.class, method = "buildUpdateSql")
    Integer updateOrderStatus(
            @Param("orderId") String orderId,
            @Param("status") String status,
            @Param("column") String column
    );

}
